/*!
 * @file NearSensor.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/NearSensor>

#include <osgODE/World>
#include <osgODE/RigidBody>
#include <osgODE/Notify>

#include <osg/UserDataContainer>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
NearSensor::NearSensor(void):
    m_distance          ( 1.0 ),
    m_reset_distance    ( 2.0 ),
    m_active            ( false )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
NearSensor::NearSensor(const NearSensor& other, const osg::CopyOp& copyop):
    Sensor              ( other, copyop ),
    m_distance          ( other.m_distance ),
    m_reset_distance    ( other.m_reset_distance ),
    m_property          ( other.m_property ),
    m_active            ( other.m_active )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
NearSensor::~NearSensor(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
namespace {
    class PropertyActorCallback: public osgODE::World::ActorCallback
    {
    public:
        PropertyActorCallback( const std::string& property ):
            m_property(property),
            m_found(false) {}

        inline bool found(void) const { return m_found ; }

        virtual bool    operator()( osgODE::ODEObject* actor )
        {
            osg::UserDataContainer* udc = actor->getUserDataContainer() ;

            if( ! udc ) {
                return false ;
            }

            osg::Object*    obj = udc->getUserObject( m_property ) ;

            if( ! obj ) {
                return false ;
            }

            m_found = true ;
            return true ;
        }

    private:
        std::string m_property ;
        bool        m_found ;
    } ;
}



/* ....................................................................... */



bool
NearSensor::pulse( osgODE::ODEObject* object, Game* game )
{

    (void) game ;

    osgODE::RigidBody*  body = object->asRigidBody() ;

    PS_ASSERT1( body != NULL ) ;

    osgODE::World*  world = body->getWorld() ;
    PS_ASSERT1( world != NULL ) ;


    bool    active = false ;
    ooReal  radius = m_active ? m_reset_distance : m_distance ;

    osg::BoundingSphere bs( body->getPosition(), radius ) ;


    if( m_property == "" ) {

        active = world->findActors( NULL, bs, 1 ) != 0 ;

    } else {

        osg::ref_ptr<PropertyActorCallback> cbk = new PropertyActorCallback( m_property ) ;

#if 0
        body->getWorld()->findActors( cbk, bs ) ;

        active = cbk->found() ;
#else


        if( world->getDirtyActors() ) {
            world->collectActors() ;
        }

        osgODE::World::ObjectList::iterator    itr = world->getActors().begin() ;
        osgODE::World::ObjectList::iterator    itr_end = world->getActors().end() ;


        while( itr != itr_end ) {

            osgODE::ODEObject*  current = *itr++ ;

            if( (*cbk)(current) ) {
                if( osgODE::World::intersectSphereSphere( current->getActorBound(), bs ) ) {

                    active = true ;
                    break ;
                }
            }
        }
#endif

    }

    m_active = active ;

    return active ;
}
/* ....................................................................... */
/* ======================================================================= */
